package cn.ijero.linechart

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import cn.ijero.linechart.ext.toNativeColor
import cn.ijero.linechart.ui.theme.*
import kotlin.random.Random


private data class TestData(
    val value: Double, val date: String
) : ILine {
    override fun dateTime(): String {
        return date
    }

    override fun value(): Double {
        return value
    }
}

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            ComposeLineChartTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colors.background
                ) {

                    val test1 = mutableListOf<TestData>()
                    val test2 = mutableListOf<TestData>()

                    val date = "20220202"
                    for (i in 0 until 20) {
                        test1.add(TestData(Random.nextDouble(50.0) - 30.0, date))
                        test2.add(TestData(Random.nextDouble(63.0) - 20.0, date))
                    }

                    val lines = listOf(
                        "博弈BS" to test1,
                        "沪深300" to test2,
                    )
                    val lineColors = listOf(
                        RedColor.toNativeColor(),
                        Color(0xFF2D6AE7).toNativeColor(),
                    )

                    val data = LineChartData(timelineCount = 5, lines = lines)
                    val yAxisTextColor = listOf(
                        RedColor.toNativeColor(), ZeroBgColor.toNativeColor(),
                        GreenColor.toNativeColor(),
                    )
                    val style = LineChartStyle(
                        showTitle = true,
                        timelineTextColor = CaptionTextColor.toNativeColor(),
                        yAxisTextColors = yAxisTextColor,
                        lineColors = lineColors
                    )
                    Box(contentAlignment = Alignment.Center) {
                        LineChart(
                            modifier = Modifier
                                .fillMaxWidth()
                                .height(180.dp),
                            data = data,
                            style = style
                        )
                    }
                }
            }
        }
    }
}
